/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Connection;

import java.sql.*;
import javax.swing.JOptionPane;

/**
 *
 * @author 2930682
 */
public class DataHandler 
{

    private String _URL, _USERNAME, _PASSWORD;
    private Connection _connection;
    private Statement _statement;
    private ResultSet _rs = null;
    private ResultSetMetaData _md;
    
    public DataHandler()
    {
        _URL = "jdbc:mysql://localhost/gr13_netbooks";
        _USERNAME = "root";
        _PASSWORD = "";
    }
    
    public void createConnection() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
    {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        _connection = DriverManager.getConnection(_URL, _USERNAME, _PASSWORD);
    }
    
    public void closeConnection() throws SQLException
    {
        _connection.close();
    }
    
    private void runQuery(String query) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
    {
        this.createConnection();
            
        _statement = _connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        _rs =  _statement.executeQuery(query);
    }
    
    public ResultSet getProducts() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
    {           
        this.runQuery("SELECT * FROM products"); 
            
        return _rs;
    }
    
}
